package org.hishenframework.core.data.jdbc.template;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

/**
 * 简单jdbc查询类
 * @author didi
 *
 * @param <T>
 */
public class HishenSimpleJdbcQuery<T> {
	
	private String tableName;
	private JdbcTemplate jdbcTemplate;
	private Class<T> entityClass;
	
	public HishenSimpleJdbcQuery(JdbcTemplate jdbcTemplate, Class<T> entityClass) {
		this.jdbcTemplate = jdbcTemplate;
		this.entityClass = entityClass;
	}
	
	public HishenSimpleJdbcQuery<T> withTableName(String tableName) {
		this.tableName = tableName;
		return this;
	}
	
	/**
	 * 查询单条记录
	 * @param args
	 * @return
	 */
	public T execute(Map<String, Object> args) {
		StringBuilder sql = new StringBuilder("SELECT * FROM ");
		sql.append(tableName).append(" WHERE ");
		List<Object> values = new ArrayList<>();
		for (Entry<String, Object> entry : args.entrySet()) {
			sql.append(entry.getKey()).append(" = ? AND ");
			values.add(entry.getValue());
		}
		sql.delete(sql.length() - 5, sql.length());
		
		RowMapper<T> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(entityClass);
		List<T> results = jdbcTemplate.query(sql.toString(), rowMapper, values.toArray(new Object[values.size()]));
		return DataAccessUtils.singleResult(results);
	}
	
	/**
	 * 查询单条记录
	 * @param args
	 * @return
	 */
	public Map<String, Object> executeForMap(Map<String, Object> args) {
		StringBuilder sql = new StringBuilder("SELECT * FROM ");
		sql.append(tableName).append(" WHERE ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> entry : args.entrySet()) {
			sql.append(entry.getKey()).append(" = ? AND ");
		}
		sql.delete(sql.length() - 5, sql.length());
		
		return jdbcTemplate.queryForMap(
				sql.toString(), values.toArray(new Object[values.size()]));
	}
	
	/**
	 * 查询多条记录
	 * @param args
	 * @return
	 */
	public List<T> executeForList(Map<String, Object> args) {
		StringBuilder sql = new StringBuilder("SELECT * FROM ");
		sql.append(tableName).append(" WHERE ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> entry : args.entrySet()) {
			sql.append(entry.getKey()).append(" = ? AND ");
		}
		sql.delete(sql.length() - 5, sql.length());
		
		return jdbcTemplate.queryForList(
				sql.toString(), entityClass, values.toArray(new Object[values.size()]));
	}
	
	/**
	 * 查询多条记录
	 * @param args
	 * @return
	 */
	public List<Map<String, Object>> executeForListMap(Map<String, Object> args) {
		StringBuilder sql = new StringBuilder("SELECT * FROM ");
		sql.append(tableName).append(" WHERE ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> entry : args.entrySet()) {
			sql.append(entry.getKey()).append(" = ? AND ");
		}
		sql.delete(sql.length() - 5, sql.length());
		
		return jdbcTemplate.queryForList(
				sql.toString(), values.toArray(new Object[values.size()]));
	}
}
